﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.Util;
using System.ComponentModel.DataAnnotations;
using WMSUIMVC.Aubit.Procurement;
using WMSUIMVC.Aubit.Sales;
using WMSUIMVC.DTO.Refund;
using XuanKeZz.Application.HTTP;

namespace WMSUIMVC.Controllers
{

    /// <summary>
    /// 销售管理
    /// </summary>
    public class SalesController : Controller
    {
        private readonly IConfiguration _configuration;

        public SalesController(IConfiguration configuration)
        {
            _configuration = configuration;
        }


        public IActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 添加销售
        /// </summary>
        /// <returns></returns>
        public IActionResult AddSales()
        {
            return View();
        }


        /// <summary>
        /// 显示销售列表
        /// </summary>
        /// <returns></returns>
        public IActionResult GetSalesList()
        {
            return View();
        }

        /// <summary>
        /// 修改销售
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdSales()
        {
            return View();
        }


        /// <summary>
        /// 销售退货
        /// </summary>
        /// <returns></returns>
        public IActionResult RefundSales()
        {
            return View();
        }


        /// <summary>
        /// 添加销售
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> AddSalesList(AddSalesDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string list = JsonConvert.SerializeObject(obj);
            string url = _configuration["Url"] + "ywx/api/Sales/AddSales";
            string res = await client.PostAsync(url, list);
            return Json(res);
        }


        /// <summary>
        /// 显示销售列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetSales(GetSalesDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"] + "ywd/api/Sales/GetSalesList?pindex=" + obj.Pindex + "&psize=" + obj.Psize + "&SalesCode=" + obj.SalesCode + "&ProductsCode=" + obj.ProductsCode + "&ProductsName=" + obj.ProductsName + "&SalesState=" + obj.SalesState;
            string res = await client.GetAsync(url);
            return Json(res);
        }



        /// <summary>
        /// 反填销售列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> FanSalesList(FanSalesDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"]+ "ywd/api/Sales/FanSales?SellProductId=" + obj.SellProductId;
            string res = await client.GetAsync(url);
            return Json(res);
        }


        /// <summary>
        /// 修改销售
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> UpdSalesList(AddSalesDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string list = JsonConvert.SerializeObject(obj);
            string url = _configuration["Url"] + "ywx/api/Sales/UpdSales";
            string res = await client.PostAsync(url, list);
            return Json(res);
        }


        /// <summary>
        /// 显示销售详情
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetSalesDetail(GetSalesDetailDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"] + "ywd/api/Sales/GetSalesDetail?SellProductId=" + obj.SellProductId;
            string res = await client.GetAsync(url);
            return Json(res);
        }


        /// <summary>
        /// 删除销售列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> DelSales(DelSalesDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"] + "ywx/api/Sales/DelSalse";
            //转成集合
            var list = "{\"sellProductId\":" + JsonConvert.SerializeObject(obj.SellProductId) + "}";
            //将集合转成json
            var res = await client.PutAsync(url, list);
            return Json(res);
        }



        /// <summary>
        /// 销售退货列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetSalesRefunds(ShowRefundSalesDto dto)
        {
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "ywd/api/Sales/GetSalesRefund?pindex=" + dto.pageIndex + "&psize=" + dto.pageSize +
                "&SalesCode=" + dto.SalesCode + "&RefundCode=" + dto.RefundCode +
                "&ProductsCode=" + dto.ProductsCode + "&ProductsName=" + dto.ProductsName +
                "&IsRefund=" + dto.isRefund + "&ClientCode=" + dto.ClientCode +
                "&StartDate=" + dto.StartDate + "&EndDate=" + dto.EndDate+ "&ClientName="+dto.ClientName;
            string Getwarehouse = await client.GetAsync(url);
            return Json(Getwarehouse);
        }


        /// <summary>
        /// 销售退货删除
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetSalesRefundsDel(DelShowRefundSalesDto dto)
        {
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "ywx/api/Refund/DelRefund";
            string list = JsonConvert.SerializeObject(dto);
            //将集合转为json
            var res = await client.PostAsync(url, list);
            string Getwarehouse = await client.PostAsync(url, JsonConvert.SerializeObject(dto));
            return Json(Getwarehouse);
        }



        /// <summary>
        ///销售退货状态修改
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetSalesRefundsUPDstate(UPDstateShowRefundSalesDto dto)
        {
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "ywx/api/Refund/UpdRefund";
            string list = JsonConvert.SerializeObject(dto);
            //将集合转为json
            var res = await client.PostAsync(url, list);
            string Getwarehouse = await client.PostAsync(url, JsonConvert.SerializeObject(dto));
            return Json(Getwarehouse);
        }

        /// <summary>
        /// 销售退货  查看 单条 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetSalesRefundViews(ShowRefundSalesViewDto dto)
        {
            HttpClientWrapper client = new HttpClientWrapper(dto.Token);
            string url = _configuration["Url"] + "ywd/api/Sales/GetSalesRefundView?RefundId="+dto.RefundId;
            string Getwarehouse = await client.GetAsync(url);
            return Json(Getwarehouse);
        }


        /// <summary>
        /// 显示操作列的退货
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetRefundDetail(GetRefundDetail obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"] + "ywd/api/Sales/GetRefundDetail?SellProductId=" + obj.SellProductId;
            string res = await client.GetAsync(url);
            return Json(res);
        }


    }
}
